package com.rongzhe.house.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.Banner;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.manager.ConfigManager;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.presenter.MainPagePresenter;
import com.rongzhe.house.ui.activity.H5BaseActivity;
import com.rongzhe.house.ui.activity.LifeServiceActivity;
import com.rongzhe.house.ui.activity.RentTypeActivity;
import com.rongzhe.house.ui.adapter.HouseAdapter2;
import com.rongzhe.house.ui.view.BannerView;
import com.rongzhe.house.ui.view.smoothlist.SmoothListView;
import com.rongzhe.house.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by huang on 2017/11/16.
 */

public class FragmentMain extends BaseFragment<MainPagePresenter> {
    @BindView(R.id.container_notification)
    LinearLayout containerNotification;
    @BindView(R.id.list_house_info)
    SmoothListView listHouseInfo;
    @BindView(R.id.real_actions_menu)
    View mRealActionsView;
    Unbinder unbinder;

    View mListHeadActionView;
    private HouseAdapter2 mAdapter;
    private boolean isScrollIdle = true; // ListView是否在滑动
    private int filterViewTopMargin; // 筛选视图距离顶部的距离
    private BannerView bannerView;

    @Override
    protected MainPagePresenter createPresenter() {
        return new MainPagePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main2, container, false);
        unbinder = ButterKnife.bind(this, view);
        mAdapter = new HouseAdapter2(getContext());
        listHouseInfo.setAdapter(mAdapter);
        bannerView = (BannerView) inflater.inflate(R.layout.main_banner_view, listHouseInfo,
                false);
        listHouseInfo.addHeaderView(bannerView);
        mListHeadActionView = inflater.inflate(R.layout.main_actions_layout, listHouseInfo,
                false);
        listHouseInfo.addHeaderView(mListHeadActionView);
        listHouseInfo.setOnScrollListener(new SmoothListView.OnSmoothScrollListener() {
            @Override
            public void onSmoothScrolling(View view) {

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                isScrollIdle = (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (mListHeadActionView != null) {
                    filterViewTopMargin = UIUtils.px2dip(getContext(), mListHeadActionView.getTop());
                }
                boolean isStickyTop = false;
                // 处理筛选是否吸附在顶部
                if (filterViewTopMargin <= 0) {
                    isStickyTop = true; // 吸附在顶部
                    mRealActionsView.setVisibility(View.VISIBLE);
                } else {
                    isStickyTop = false; // 没有吸附在顶部
                    mRealActionsView.setVisibility(View.GONE);
                }
            }
        });
        setOnClickListener(mRealActionsView);
        setOnClickListener(mListHeadActionView);
        return view;
    }

    private void launchTypeActivity(String type) {
        Intent intent = new Intent(getContext(), RentTypeActivity.class);
        intent.putExtra(RentTypeActivity.EXTRA_TITLE, type /**/);
        launchActivity(false, intent);
    }

    public void setOnClickListener(View view) {
        view.findViewById(R.id.btn_rent_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseManager.getInstance().setAsEntire(false);
                launchTypeActivity(getString(R.string.rent_together));
            }
        });
        view.findViewById(R.id.btn_rent_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseManager.getInstance().setAsEntire(true);
                launchTypeActivity(getString(R.string.rent_all));
            }
        });
        view.findViewById(R.id.btn_rent_direct).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseManager.getInstance().setAsRentDirect(true);
                launchTypeActivity(getString(R.string.rent_direct));
            }
        });
        view.findViewById(R.id.btn_life_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = H5BaseActivity.getH5BaseActivityIntent(getContext(),
                        getString(R.string.life_service),
                        ConfigManager.getInstance().getServiceLifePage());
                launchActivity(true, intent);
            }
        });
        view.findViewById(R.id.btn_rent_apartment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseManager.getInstance().setAsApartment(true);
                launchTypeActivity(getString(R.string.rent_apartment));
            }
        });
    }

    public void updateUi() {
        mAdapter.updateUi();
    }

    public void setRentAllList(List<HouseOverview> mRentAllList) {
        mAdapter.setRentAllList(mRentAllList);
    }

    public void setRentApartmentList(List<HouseOverview> mRentApartmentList) {
        mAdapter.setRentApartmentList(mRentApartmentList);
    }

    public void setRentShareList(List<HouseOverview> mRentShareList) {
        mAdapter.setRentShareList(mRentShareList);
    }

    public void setRentDirectList(List<HouseOverview> mRentDirectList) {
        mAdapter.setRentDirectList(mRentDirectList);
    }

    public void setRentRecommend(List<HouseOverview> mRentRecommend) {
        mAdapter.setRentRecommend(mRentRecommend);
    }


    public void setBannerImage(List<Banner> data) {
        if (data == null || data.size() < 1) {
            return;
        }
        List<BannerView.BannerData> showingBannerDatas = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            showingBannerDatas.add(new BannerView.BannerData(-1, data.get(i).getUrl(),
                    data.get(i).getBizContent()));
        }
        if (showingBannerDatas.size() > 0) {
            bannerView.setDatas(showingBannerDatas);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
